import React, { Component } from 'react'
import { Button, Table, Modal, Row, Col, Popconfirm, Input, Form, message } from 'antd';
import { connect } from 'react-redux';
import { findNoPost, findNoPostByNumber, getAllMetaDataTypes, getResourceInfo, getResourceTableInfo, deleteResourceById} from '../../../api/resourceCategory/resourceCategory'
import { actionCreators} from './store'
import CommonView from '../commonView'
import EditResource from './editResource'
// import { actionCreators } from './store';
const footerStyle={
  width: '500', margin: 'auto'
  // backgroundColor:'#e6eef5',width:520,height:53,position:'absolute',left:0,bottom:0,
  // display:'flex',alignItems:'center',justifyContent:'center'
}
class UnSubmit extends Component {
  constructor() {
    super()
    this.state = {
      dataSource: [],
      metedataTypes: [],
      resourceId: '',
      passDataSource: [],
      passBaseInfo: {},
      sendVis: false,//报送显示
      editVis: false,//编辑
      paginationNo: {
        pageSize: 10,
        defaultCurrent:1,
        current:1,
      },
      columns : [
        {
          title: '操作', dataIndex: 'action', key: 'action', width: 220,
          render: (text, record) => {
            return (
              <div>
                <Button size="small" type="link" onClick={() => this.send(record)}>报送</Button>
                <Button size="small" type="link" onClick={() => this.handleEditClick(record)} >编辑</Button>
                <Popconfirm
                  title="确认删除?" okText="确定" cancelText="取消"
                  onConfirm={() => this.handleDeleteClick(record)}
                >
                  <Button size="small" type="link" style={{ marginLeft: 10 }}>删除</Button>
                </Popconfirm>
              </div>
            )
          }
        }
      ]
    }
  }
  handleEditClick(record) {
    console.log('edit', record)

    this.setState({
      categoryNumber: record.catalogue_number,
    })
    //编辑前获取数据
    this.getInfo(record, 1)
    this.props.setCateregEditShow(true)
  }

  getInfo = (record, type) => { //type为0为报送的获取信息，type为1为编辑的获取信息
    let requestData = {resourceId: record.resource_id,}
    let passInfo = {}
    getResourceInfo(requestData).then(res => {
      if(res.data.metaData.code === 'success') {
        passInfo = res.data.data
        this.setState({passBaseInfo: res.data.data})
        this.props.setFormInitialData(res.data.data)
      }
    })
    getResourceTableInfo(requestData).then(res => {
      if(res.data.metaData.code === 'success') {
        type === 0 ? this.setState({passBaseInfo: passInfo, passDataSource: res.data.data,sendVis: true})
            :this.setState({passBaseInfo: passInfo,passDataSource: res.data.data, editVis: true})
      }
    })
    if(type === 1) {
      getAllMetaDataTypes({number: record.catalogue_number}).then(res => {
        if(res.data.metaData.code === 'success') {
          this.setState({metedataTypes: res.data.data})
        }
      })
    }
  }
  send = record => {
    console.log('send', record)
    this.setState({resourceId: record.resource_id})
    this.getInfo(record, 0)
  }

  view = record => {
    console.log('view',record)
  }
  getTaskList = () => {
  }

  getNoPost = (number, page, pageSize) => {
    findNoPostByNumber({pageNum: page, pageSize: pageSize, number:number,type:'1'}).then(res => {
      if(res.data.metaData.code === 'success') {
        const paginationNo = this.state.paginationNo
        paginationNo.total = res.data.data.total
        paginationNo.current = res.data.data.pageNum
        this.setState({dataSource: res.data.data.list})
      }
    })
  }
  componentWillMount() {
    this.setState({
      columns: [...this.props.baseColumns ,...this.state.columns]
    })
    // this.getNoPost(this.props.curNumber, 1,10)
    this.getNoPost('01010000', 1,10)
  }

  componentDidUpdate(prevProps) {
    if(prevProps.curNumber !== this.props.curNumber) {
      console.log('did update')
      this.getNoPost(this.props.curNumber, 1, 10)
    }
  }

  handleOk = e => {
    this.setState({editVis: false, sendVis: false})
    // this.props.setCateregEditShow(false)
  }

  handleSend =() => {
    let obj ={resourceId: this.state.resourceId}
    this.props.sendResPost(obj)
  }

  //删除配置
  handleDeleteClick = (record) =>  {
    console.log('delete', record)
    let obj = {resourceId: record.resource_id}
    deleteResourceById(obj).then(res => {
      if(res.data.metaData.code === 'success') {
        message.success('删除成功')
      }
    })
    // this.props.deleteAnalyseTask({code: code})
  }
  render() {
    console.log('receive from parent', this.props.curNumber, this.props.type)
    const  { curRecord, dataSource, resourceId, passBaseInfo, passDataSource, editVis, sendVis  } = this.state
    return (
      <div>
          <Table
            size="small" loading={this.state.loading} rowKey="index"
            dataSource={dataSource}
            columns={this.state.columns} bordered 
            style={{ padding: 20 }}
            pagination={this.state.paginationNo} />
          <Modal title={'报送确认'} visible={sendVis} width={700} onCancel={e => this.setState({sendVis: false})}
            footer={null}
            bodyStyle={{backgroundColor:'#f0f3f6',overflow:'auto',height:540}}
          >
            <div>
            <CommonView resourceId={ resourceId} dataSource={passDataSource} infos={passBaseInfo} />
            <div style={{padding: '0px 20px 0px 20px',  backgroundColor: '#fff',   margin: '24px 16px 0 16px '}}>
              <div style={{ width:'700px', margin:'auto'}}>
                <Button type="primary"  size='small' ghost onClick={e => this.setState({sendVis: false})}>取消</Button>
                <Button type="primary" style={{marginLeft: '30px'}} onClick={this.handleSend} size='small' >报送</Button>
              </div>
            </div>

            </div>
          </Modal>
          <Modal title={'编辑资源'}
            visible={editVis} width={800}
            onOk={this.handleOk} onCancel={this.handleOk} closable={true}
            footer={null}
            >
            <EditResource curNumber={this.state.categoryNumber} onRef={ref=>this.editData=ref} passDataSource={passDataSource}
               metedataTypes={this.state.metedataTypes} type="unSubmitEdit"  />
          </Modal>
      </div>
    )
  }
}



const mapState = (state) => {
  return {
     editShow: state.getIn(['catemanage', 'editShow'])
  }
}

const mapDispatch = (dispatch) => {
  return {
    deleteCateInfo(data) {
      dispatch(actionCreators.deleteCateInfo(data))
    },
    getCateResourceInfo(data) {
      dispatch(actionCreators.getCateResourceInfo(data))
    },
    getCateResMetaInfo(data) {
      dispatch(actionCreators.getCateResMetaInfo(data))
    },
    sendResPost(data) {
      dispatch(actionCreators.sendResPost(data))
    },
    setFormInitialData(data) {
      console.log('data', data)
      dispatch(actionCreators.setFormInitialData(data))
    },
    setCateregEditShow(value) {
      dispatch(actionCreators.setCateregEditShow(value))
    }
  }
} 
export default connect(mapState, mapDispatch)(UnSubmit)


